﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using System.Web.Security;

namespace TireService.Website.Kunde
{
    public partial class Hjulskift : System.Web.UI.Page
    {
        #region Public Properties

        public int CustomerId
        {
            get
            {
                if (ViewState["_customerid"] != null)
                    return (int)ViewState["_customerid"];
                else
                    return (int)Session["CustomerId"];
            }
            set
            {
                ViewState["_customerid"] = value;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            // Test propertie
            //this.CustomerId = 10001;

            if (Page.IsPostBack != true)
            {
                
            }

            BindServiceCarScheduleDetail(db, this.CustomerId);
        }

        /// <summary>
        /// Function that bind scheduledetail data to gridview
        /// </summary>
        /// <param name="db">Connection to database</param>
        /// <param name="CustomerId">Id on customer</param>
        private void BindServiceCarScheduleDetail(TireServiceDB db, int CustomerId)
        {
            gvServiceCarScheduleDetail.DataSource = db.ServiceCarScheduleDetailViews.Where(p => p.CustomerId == CustomerId).ToList();
            gvServiceCarScheduleDetail.DataBind();

            gvServiceCarScheduleDetail.ClearSort();
            gvServiceCarScheduleDetail.GroupSummary.Clear();
            gvServiceCarScheduleDetail.GroupBy(gvServiceCarScheduleDetail.Columns["CarBrandDescription"]);
            gvServiceCarScheduleDetail.ExpandAll();
        }

        public decimal GetCustomerRating(bool CustomerRated, int ServiceCarScheduleDetailId)
        {
            if (CustomerRated)
            {
                TireServiceDB db = new TireServiceDB();

                decimal rating = Convert.ToDecimal(db.GetServiceCarScheduleDetailRating(ServiceCarScheduleDetailId));

                return rating;
            }
            else
            {
                return 0;
            }
        }
    }
}